﻿using System;
using System.Text;

namespace Edu.Technion.CSE.OpenUnit4.CollectionsLib
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Queue<T> : IQueue<T>
    {
        // Members

        private System.Collections.Generic.Queue<T> queue;


        // Constructors

        /// <summary>
        /// 
        /// </summary>
        public Queue()
        {
            queue = new System.Collections.Generic.Queue<T>();
        }

        // Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public T Head()
        {
            return queue.Peek();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Insert(T item)
        {
            queue.Enqueue(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return (queue.Count == 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public T Remove()
        {
            T result = default(T);
            if (!IsEmpty())
            {
                result = queue.Dequeue();
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("[");
            T[] items = queue.ToArray();
            for (int i = 0; i < items.Length; i++)
            {
                sb.Append(items[i].ToString());
                sb.Append(",");
            }
            sb.Insert(sb.Length - 1, "]");
            return sb.ToString();
        }
    }
}
